package com.jier.common;

import com.jier.common.util.StringUtil;

import java.io.Serializable;

/**
 * 分页类
 *
 * @author jie_r
 */
public class Page implements Serializable {
    /**
     * 默认开始页
     */
    public final static Integer START = 1;
    /**
     * 默认行数
     */
    public final static Integer LIMIT = 15;
    /**
     * 每页的行数
     */
    private int rowPerPage;
    /**
     * 当前的页数
     */
    private int curPage;
    /**
     * 总行数
     */
    private int totalRows;
    /**
     * 总页数
     */
    private int totalPages;
    /**
     * 当前页开始行索引
     */
    private int startRowIndex;

    public Page() {
        this.rowPerPage = START;
    }

    public Page(int rowPerPage) {
        this.rowPerPage = rowPerPage;
    }

    public Page(String start, String limit) {
        // 如果start为null，或者格式错误，默认设置为1
        this.startRowIndex = StringUtil.parseInt(start, START);
        // 如果limit为null，或者格式错误，默认设置为20
        this.rowPerPage = StringUtil.parseInt(limit, LIMIT);
    }

    public Page(Integer start, Integer limit) {
        this.startRowIndex = start == null ? START : start;
        this.rowPerPage = limit == null ? LIMIT : limit;
    }

    /**
     * 根据查询到的总行数分页
     */
    public Page pagging(int totalRows) {
        this.totalRows = totalRows;
        return this;
    }

    public int getRowPerPage() {
        return this.rowPerPage;
    }

    public void setRowPerPage(int rowPerPage) {
        this.rowPerPage = rowPerPage;
    }

    public int getCurPage() {
        return this.curPage;
    }

    public void setCurPage(int curPage) {
        this.curPage = curPage;
    }

    public int getTotalRows() {
        return this.totalRows;
    }

    public void setTotalRows(int totalRows) {
        this.totalRows = totalRows;
    }

    public int getTotalPages() {
        return this.totalPages;
    }

    public void setTotalPages(int totalPages) {
        this.totalPages = totalPages;
    }

    public int getStartRowIndex() {
        return this.startRowIndex;
    }

    public void setStartRowIndex(int startRowIndex) {
        this.startRowIndex = startRowIndex;
    }
}
